package com.ruoyi.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.base.domain.BaseRiver;
import com.ruoyi.base.mapper.BaseRiverMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BusWaterSegmentMapper;
import com.ruoyi.base.domain.BusWaterSegment;
import com.ruoyi.base.service.IBusWaterSegmentService;

/**
 * 基本断面Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class BusWaterSegmentServiceImpl implements IBusWaterSegmentService {
    @Autowired
    private BusWaterSegmentMapper busWaterSegmentMapper;
    @Autowired
    private BaseRiverMapper baseRiverMapper;

    /**
     * 查询基本断面
     *
     * @param ID 基本断面主键
     * @return 基本断面
     */
    @Override
    public BusWaterSegment selectBusWaterSegmentByID(String ID) {
        return busWaterSegmentMapper.selectBusWaterSegmentByID(ID);
    }

    /**
     * 查询基本断面列表
     *
     * @param busWaterSegment 基本断面
     * @return 基本断面
     */
    @Override
    public List<BusWaterSegment> selectBusWaterSegmentList(BusWaterSegment busWaterSegment) {
        return busWaterSegmentMapper.selectBusWaterSegmentList(busWaterSegment);
    }

    /**
     * 新增基本断面
     *
     * @param busWaterSegment 基本断面
     * @return 结果
     */
    @Override
    public int insertBusWaterSegment(BusWaterSegment busWaterSegment) {
        busWaterSegment.setId(UUID.randomUUID().toString());
        if(StringUtils.isNotEmpty(busWaterSegment.getIsauto()) && "1".equals(busWaterSegment.getIsauto())){
            busWaterSegment.setIsmanual("0");
        }
        if(StringUtils.isNotEmpty(busWaterSegment.getIsmanual()) && "1".equals(busWaterSegment.getIsmanual())){
            busWaterSegment.setIsauto("0");
        }
        if(StringUtils.isNotEmpty(busWaterSegment.getRiverid())){
            BaseRiver baseRiver= baseRiverMapper.selectBaseRiverByID(busWaterSegment.getRiverid());
            busWaterSegment.setRivername(baseRiver.getRivername());
        }

        busWaterSegment.setCreatetime(new Date());
        return busWaterSegmentMapper.insertBusWaterSegment(busWaterSegment);
    }

    /**
     * 修改基本断面
     *
     * @param busWaterSegment 基本断面
     * @return 结果
     */
    @Override
    public int updateBusWaterSegment(BusWaterSegment busWaterSegment) {
        if(StringUtils.isNotEmpty(busWaterSegment.getIsauto()) && "1".equals(busWaterSegment.getIsauto())){
            busWaterSegment.setIsmanual("0");
        }
        if(StringUtils.isNotEmpty(busWaterSegment.getIsmanual()) && "1".equals(busWaterSegment.getIsmanual())){
            busWaterSegment.setIsauto("0");
        }
        if(StringUtils.isNotEmpty(busWaterSegment.getRiverid())){
            BaseRiver baseRiver= baseRiverMapper.selectBaseRiverByID(busWaterSegment.getRiverid());
            busWaterSegment.setRivername(baseRiver.getRivername());
        }

        busWaterSegment.setUptime(new Date());
        return busWaterSegmentMapper.updateBusWaterSegment(busWaterSegment);
    }

    /**
     * 批量删除基本断面
     *
     * @param IDs 需要删除的基本断面主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterSegmentByIDs(String[] IDs) {
        return busWaterSegmentMapper.deleteBusWaterSegmentByIDs(IDs);
    }

    /**
     * 删除基本断面信息
     *
     * @param ID 基本断面主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterSegmentByID(String ID) {
        return busWaterSegmentMapper.deleteBusWaterSegmentByID(ID);
    }
}
